<template>
  <div class="app-container">
    <u-page-config
      :filter-array="filterArray"
      v-model="filter"
      :actionBtn="actionBtn"
      label-width="100px"
      :getList="getList"
      :tableData="tableData"
      :tableColumn="tableColumn"
      :total="total"
      :loading="loading"
      showSelection
      @handleSelectionChange="handleSelectionChange"
      @addRow="addRow"
      @removeRow="removeRow"
      @editRow="editRow"
      @rowDetail="rowDetail"
      @export="handleExport"
    >

    </u-page-config>

    <el-dialog :title="title" :visible.sync="visible" width="600px" :close-on-click-modal="false">
      <u-form
        ref="uFormRef"
        label-width='134px'
        v-model="form"
        :defaultForm="defaultForm"
        :formArray="formArray"
        :show-btn="false"
        @submit="onsubmit"
        @close="visible = false"
        :mode="mode"
      >
      </u-form>
    </el-dialog>
  </div>
</template>

<script>
import UPageConfig from '@/components/uPageConfig/index'
import UForm from '@/components/uForm'
import table from "@/mixin/uTable";
import Treeselect from "@riophae/vue-treeselect";
import {
  getBaseStepList,
  addStep,
  delStep,
  updateStep,
} from "@/api/items/serviceSteps";

export default {
  name: "UpdatePlan",
  mixins: [table],
  dicts: ['step_type'],
  components: {UPageConfig, UForm, Treeselect},
  data() {
    return {
      filter: {
        pageSize: 10,
        pageNum: 1,
        data: {
          branchCode: undefined
        }
      },
      filterArray: [
        {label: "步骤名称", key: "stepName", type: "input"},
        {label: "步骤组件名称", key: "componentName", type: "input"},
        {label: "步骤类型", key: "type", type: "select"},
      ],
      tableColumn: [
        {label: "步骤名称", prop: "stepName"},
        {label: "步骤组件名称", prop: "componentName"},
        {label: "步骤类型", prop: "type", type: 'select', options: [], width: 140},
        {label: "备注", prop: "remark", showOverflowTooltip: true},
        {
          label: "操作", type: "btn", attr: {width: 180}, btnArr: [
            {
              icon: "el-icon-edit-outline",
              text: "修改",
              emit: "editRow",
              permissions: 'items:serviceSteps:edit',
            },
            {
              icon: "el-icon-delete",
              text: "删除",
              emit: "removeRow",
              permissions: 'items:serviceSteps:remove'
            },
          ]
        }
      ],
      actionBtn: [
        {
          type: 'primary',
          text: '创建',
          icon: 'el-icon-plus',
          emit: 'addRow',
          permissions: 'items:serviceSteps:add'
        },
        {
          type: 'success',
          text: '修改',
          icon: 'el-icon-edit',
          emit: 'editRow',
          permissions: 'items:serviceSteps:edit',
          disabledRule: () => this.ids.length !== 1
        },
        {
          type: 'danger',
          text: '删除',
          icon: 'el-icon-delete',
          emit: 'removeRow',
          permissions: 'items:serviceSteps:remove',
          disabledRule: () => this.ids.length === 0
        },
        {
          type: 'warning',
          text: '导出',
          icon: 'el-icon-download',
          emit: 'export',
          permissions: 'items:serviceSteps:export'
        },
      ],
      form: {
        sort: 0
      },
      // 表单默认必填参数 重置表单时需要用到
      defaultForm: {
        stepName: null,
        componentName: null,
        type: null,
        sort: 0,
        remark: null
      },
      formArray: [
        {
          label: "步骤名称: ", key: "stepName", col: 24, type: "input",
          rule: {required: true, message: '步骤名称不能为空！', trigger: 'blur'},
        },
        {
          label: "步骤组件名称: ", key: "componentName", col: 24, type: "input",
          rule: {required: true, message: '步骤组件名称不能为空！', trigger: 'blur'},
        },
        {
          label: "步骤类型: ", key: "type", col: 24, type: "select",
          rule: {required: true, message: '步骤类型不能为空！', trigger: 'blur'},
        },
        {
          label: "排序号: ", key: "sort", col: 24, type: "number",
        },
        {
          label: "备注: ", key: "remark", col: 24, type: "textarea",
          placeholder: "查询类步骤请备注所属单位名称+事项名称"
        },
      ],
    };
  },
  computed: {
    // 网点树形数据
    branchDataOptions() {
      return this.$store.state.device.branchDataOptions || []
    },
  },
  methods: {
    /** 转换分组树形图数据结构 */
    branchNormalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.branchCode,
        label: node.branchName,
        children: node.children || undefined,
      };
    },
    // 表单提交接口相关配置
    formSubmitFunConfig() {
      return {
        addFunc: addStep,
        modifyFunc: updateStep,
        pk: 'id',
        options: {
          ...this.form,
          deploymentCode: "deploymentCode"
        },
        cbFunc: () => {
          this.visible = false
        }
      }
    },
    // 删除
    removeRow(row) {
      const ids = row ? [row.id] : this.ids;
      this.removeTip(async () => {
        await delStep(ids)
      }, row && row.id ? '该项数据' : '已勾选项数据')
    },
    // 添加
    addRow() {
      this.mode = 'form';
      this.title = "添加事项步骤";
      this.visible = true;
    },
    // 修改
    async editRow(row) {
      console.log(row, 'row');
      this.mode = 'form'
      this.title = "修改事项步骤";
      if (row) { //点击的表格内修改按钮
        this.form = JSON.parse(JSON.stringify(row))
      } else { //点击表格上方修改按钮
        const selectedItem = this.tableData.find(item => this.ids[0] === item.id) || {}
        this.form = JSON.parse(JSON.stringify(selectedItem))
      }
      this.visible = true
      await this.$nextTick()
    },
    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
    },
    // 添加文件上传成功、删除图片时都会被调用
    imageUploadChange(imgUrl) {
      this.form.pic = imgUrl
    },
    /** 查询工单列表 */
    getList() {
      this.getInit(getBaseStepList, {})
    },
    /** 详细按钮操作 */
    rowDetail(row) {
      this.mode = 'text'
      // 直接拿到这一行的数据赋值
      this.form = row;
      this.visible = true
      this.title = '详细信息'
      // 如果当前行数据不完整 调接口拿详情数据
      // this.getDetail('详细信息', WorkOrderDetail, row.id)
    },
    /** 导出按钮操作 */
    handleExport() {
      const currentDate = this.parseTime(new Date());
      const params = this.getListRequestParams();
      this.download2(
        "/baseStep/exportStep",
        params,
        `事项步骤_${currentDate}.xlsx`,
      );
    },
  },
  watch: {
    'dict.type.step_type': {
      handler(newValue) {
        // 设置设备状态选项数据
        try {
          const options = newValue.map(item => {
            item.value = Number(item.value)
            return item
          })
          this.filterArray.find(item => item.key === 'type').options = options;
          this.formArray.find(item => item.key === 'type').options = options;
          this.tableColumn.find(item => item.prop === 'type').options = options;
        } catch (e) {
          console.log("找不到对应表单项", e)
        }
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.form-item-wrap {
  display: flex;
}
</style>
